<!doctype html>
<html lang="en" data-bs-theme="auto">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>产品管理</title>

  <link href="/assets/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/assets/icons/font/bootstrap-icons.css">
  <link rel="stylesheet" href="/libs/tree/select-tree.css">
 
  <link href="/css/dashboard.css" rel="stylesheet">
  <script src="/js/jquery.min.js"></script>
  <script src="/libs/tree/select-tree.js"></script>
 
</head>

<body>

  <div class="container-fluid">

    <h5 class="card-title mt-2">产品管理</h5>
    <hr>
    <div class="row p-3">
      <div class="col-5">
        <div class="col-sm-10">
          <select class="form-select" id="smallid" name="smallid"></select>
        </div>
      </div>
      <div class="col-5">
        <div class="input-group">
        <input type="text" class="form-control" name="searchkeyword" id="searchkeyword" value="<%c echo<<obj["searchword"].to_string(); %>" placeholder="">
          <button class="btn btn-outline-secondary" type="button" id="button-addon2" onclick="searcharticle()">搜索</button>
        </div> 
      </div>  
    </div>  
    <div class="row p-3">
      <div class="col-10">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">分类</th>
              <th scope="col">标题</th>
              <th scope="col">图片</th>
              <th scope="col">排序</th>
              <th scope="col">显示</th>
              <th scope="col">精品</th>
              <th scope="col">首页</th>
              <th scope="col">操作</th>
            </tr>
          </thead>
          <tbody>
            <%c for(auto &a:obj["alist"].as_array()){ %>
            <tr id="article_<%c echo<<a.second["pid"].to_string(); %>">
              <td><%c echo<<a.second["pid"].to_string(); %></td>
              <td><%c echo<<a.second["cname"].to_string(); %></td>
              <td><%c echo<<a.second["name"].to_string(); %></td>
              <td><img src="<%c echo<<a.second["bigimg"].to_string(); %>" width="60" height="60"/></td>
              <td>
              <input type="text" class="form-inline topicsort col-md-2" id="title_<%c echo<<a.second["id"].to_string(); %>" name="title_<%c echo<<a.second["id"].to_string(); %>" value="<%c echo<<a.second["sortid"].to_string(); %>" onchange="updatearticlesort(<%c echo<<a.second["pid"].to_string(); %>,this.value)">
              </td>
              <td><input class="form-check-input" type="checkbox" value="<%c echo<<a.second["pid"].to_string(); %>" id="art_<%c echo<<a.second["pid"].to_string(); %>" onclick="artupdateview(this)" <%c if(a.second["isview"].to_int()==1){ %>checked<%c } %> >
</td>
<td><input class="form-check-input" type="checkbox" value="<%c echo<<a.second["pid"].to_string(); %>" id="art_<%c echo<<a.second["pid"].to_string(); %>" onclick="artupdateisstore(this)" <%c if(a.second["isstore"].to_int()==1){ %>checked<%c } %> >
</td>
</td>
<td><input class="form-check-input" type="checkbox" value="<%c echo<<a.second["pid"].to_string(); %>" id="home_<%c echo<<a.second["pid"].to_string(); %>" onclick="homeupdateisstore(this)" <%c if(a.second["ishome"].to_int()==1){ %>checked<%c } %> >
</td>
              <td>
              <a href="/admin/editproduct?id=<%c echo<<a.second["pid"].to_string(); %>">编辑</a>｜
                  <a href="/admin/deleteproduct?id=<%c echo<<a.second["pid"].to_string(); %>" onclick="return deletearticle(<%c echo<<a.second["pid"].to_string(); %>)">删除</a></td>
            </tr>
            <%c } %>

          </tbody>

        </table>
      </div>
    </div>

    <div class="row p-3">
      <div class="col-10">
        <nav aria-label="Page navigation example">
          <ul class="pagination">
          <%c if(obj["pageinfo"]["min"].to_int()>1){ %>
            <li class="page-item"><a class="page-link" href="/admin/listproduct?categoryid=<% echo<<obj["categoryid"].to_int(); %>&page=<% echo<<obj["pageinfo"]["current"].to_int(); %>">1</a></li>
          <%c } %>
          <%c if(obj["pageinfo"]["current"].to_int()>1){ %>
            <li class="page-item"><a class="page-link" href="/admin/listproduct?categoryid=<% echo<<obj["categoryid"].to_int(); %>&page=<% echo<<(obj["pageinfo"]["current"].to_int()-1); %>">上一页</a></li>
          <%c } %>
            
            <%c unsigned int maxpage=obj["pageinfo"]["max"].to_int(); %>
            <%c unsigned int currentpage=obj["pageinfo"]["current"].to_int(); %>
            <%c for(unsigned int i=obj["pageinfo"]["min"].to_int();i<=maxpage;i++){ %>
            <%c if(i==currentpage){ %>
            <li class="page-item"><a class="page-link active" href="/admin/listproduct?categoryid=<% echo<<obj["categoryid"].to_int(); %>&page=<% echo<<std::to_string(i); %>"><%c echo<<std::to_string(i); %></a></li>
            <%c }else{ %>
            <li class="page-item"><a class="page-link" href="/admin/listproduct?categoryid=<% echo<<obj["categoryid"].to_int(); %>&page=<% echo<<std::to_string(i); %>"><%c echo<<std::to_string(i); %></a></li>
            <%c } %>
            <%c } %>
            <%c if(obj["pageinfo"]["current"].to_int()<obj["pageinfo"]["total"].to_int()){ %>
            <li class="page-item"><a class="page-link" href="/admin/listproduct?categoryid=<% echo<<obj["categoryid"].to_int(); %>&page=<% echo<<(obj["pageinfo"]["current"].to_int()+1); %>">下一页</a></li>
            <%c } %>
            <%c if(obj["pageinfo"]["total"].to_int()>obj["pageinfo"]["max"].to_int()){ %>
            <li class="page-item"><a class="page-link" href="/admin/listproduct?categoryid=<% echo<<obj["categoryid"].to_int(); %>&page=<% echo<<obj["pageinfo"]["total"].to_int(); %>"><% echo<<obj["pageinfo"]["total"].to_string(); %></a></li>
            <%c } %>
          
          </ul>
        </nav>
      </div>
    </div>

  </div>

  <script src="/assets/dist/js/bootstrap.bundle.min.js"></script>
    <script lang="javascript">
 
    var categoryid=<% echo<<obj["categoryid"].to_int(); %>;
    var category_json = <% echo << obj["categorylist"].to_json(); %> ;

    $('#smallid').append($('<option>').val("0").text("产品分类").attr("selected", true));
    for (var i = 0; i < category_json.length; i++) 
    {
      if(category_json[i].id==categoryid)
      {
        $('#smallid').append($('<option>').val(category_json[i].id).text(category_json[i].value).data("pid", category_json[i].parentid).attr("selected","selected"));
      }
      else
      {      
        $('#smallid').append($('<option>').val(category_json[i].id).text(category_json[i].value).data("pid", category_json[i].parentid));
      }
    }

    var categoryselect =$("#smallid").selectTree({ expandLevel: 1,
        changeCallBack:function(data){
          categoryid=data[0].value;
        }
    });

    function deletearticle(id)
    {
      if(confirm('是否删除?'))
      {          
          $.getJSON("/admin/deleteproductajax?id="+id,function(result){
                if(result.code==0)
                {
                     $("#article_"+id).remove();
                }
                
          });

      }
      return false;
    }
    function updatearticlesort(id,sortid)
    {
       $.getJSON("/admin/updateproductsort?id="+id+"&sortid="+sortid,function(result){
                               
       });
    }
    function searcharticle()
    {
        if($("#searchkeyword").val()<1)
        {
           alert("请输入关键词！");
        }
        window.location.href="/admin/listproduct?categoryid="+categoryid+"&searchword="+$("#searchkeyword").val();
    }
    function artupdateview(obj)
    {
      let isview=0;
      if(obj.checked)
        {
           isview=1;
        }
        $.post("/admin/updateproductview?id="+obj.value,{isview:isview},function(result){
                               
       });
    }
    function artupdateisstore(obj)
    {
      let isstore=0;
      if(obj.checked)
        {
          isstore=1;
        }
        $.post("/admin/updateproductstore?id="+obj.value,{isstore:isstore},function(result){
                               
       });
    }
    function homeupdateisstore(obj)
    {
      let ishome=0;
      if(obj.checked)
        {
          ishome=1;
        }
        $.post("/admin/updateproducthome?id="+obj.value,{ishome:ishome},function(result){
                               
       });
    }
    </script>
</body>

</html>